package com.lv.fast.util;

import cn.hutool.core.bean.BeanUtil;
import com.lv.fast.DTO.SysUserDTO;
import com.lv.fast.VO.SysUserVO;
import com.lv.fast.constant.RestResultCodeConstant;
import com.lv.fast.constant.RestResultDescribeConstant;
import com.lv.fast.enums.RestResultEnum;
import com.lv.fast.enums.UserStatusEnum;
import com.lv.fast.exception.MyException;
import com.lv.fast.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author jie.lv
 */
public class SysUserUtil {

    private SysUserUtil(){}

    private static final ConcurrentHashMap<String, String> USER_PASSWORD = new ConcurrentHashMap<>();

    private static final ConcurrentHashMap<String, SysUserVO> USER_INFO = new ConcurrentHashMap<>();

    public static SysUserVO getCurrentLoginUser(){
        SysUserVO sysUserVO = (SysUserVO) SecurityUtils.getSubject().getPrincipal();
        Assert.assertNotNull(sysUserVO, "获取当前登录用户信息失败");
        return sysUserVO;
    }

    public static String getCurrentLoginUserId(){
        SysUserVO currentLoginUser = getCurrentLoginUser();
        String id = currentLoginUser.getId();
        Assert.assertNotNull(id, "获取当前登录用户id失败");
        return id;
    }

    public static String getUserPassword(String userId){
        Assert.assertNotNull(userId, "用户Id不能为空");
        String password = USER_PASSWORD.get(userId);
        if (StringUtils.isBlank(password)){
            password =  getUserByDataBase(userId, false).getPassword();
            removeUserPassword(userId);
            addUserPassword(userId, password);
        }
        Assert.assertNotNull(password, RestResultCodeConstant.UNKNOWN, "获取用户信息异常");
        return password;
    }

    private static SysUserDTO getUserByDataBase(String userId, boolean isContainsRoles){
        //从数据库获取
        SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class);
        SysUserDTO sysUser = sysUserService.getSysUserById(userId, isContainsRoles);
        Assert.assertNotNull(sysUser, RestResultCodeConstant.DATABASE_NOT_EXIST, RestResultDescribeConstant.DATABASE_NOT_EXIST);
        String status = sysUser.getStatus();
        if (StringUtils.isBlank(status) || status.equals(UserStatusEnum.DELETED.getCode())){
            throw new MyException(RestResultEnum.DATABASE_NOT_EXIST_ERROR);
        }
        if (status.equals(UserStatusEnum.LOCK.getCode())){
            throw new MyException(RestResultEnum.USER_LOCK);
        }
        return sysUser;
    }

    public static boolean removeUserPassword(String userId){
        Assert.assertNotNull(userId, "用户Id不能为空");
        USER_PASSWORD.remove(userId);
        return true;
    }

    public static boolean addUserPassword(String userId, String password){
        Assert.assertNotNull(userId, "用户Id不能为空");
        Assert.assertNotNull(password, "用户密码不能为空");
        USER_PASSWORD.put(userId, password);
        return true;
    }

    public static SysUserVO getUserInfoByToken(String token){
        Assert.assertNotNull(token, "token不能为空");
        String userId = JwtUtil.getTokenUserId(token);
        return getUserInfo(userId);
    }

    public static SysUserVO getUserInfo(String userId){
        Assert.assertNotNull(userId, "用户Id不能为空");
        SysUserVO sysUser = USER_INFO.get(userId);
        if (sysUser == null){
            sysUser = BeanUtil.copyProperties(getUserByDataBase(userId, true), SysUserVO.class);
            Assert.assertNotNull(sysUser, RestResultCodeConstant.DATABASE_NOT_EXIST, RestResultDescribeConstant.DATABASE_NOT_EXIST);
            removeUser(userId);
            addUser(sysUser);
        }
        return sysUser;
    }

    public static boolean removeUser(String userId){
        Assert.assertNotNull(userId, "用户Id不能为空");
        USER_INFO.remove(userId);
        return true;
    }

    public static boolean addUser(SysUserVO sysUser){
        Assert.assertNotNull(sysUser, "用户对象不能为空");
        USER_INFO.put(sysUser.getId(), sysUser);
        return true;
    }
}
